package org.linlinjava.litemall.db.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.linlinjava.litemall.db.dao.LitemallAdminMapper;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.domain.LitemallAdmin.Column;
import org.linlinjava.litemall.db.domain.LitemallAdminExample;
import org.linlinjava.litemall.db.domain.LitemallRole;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.util.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LitemallAdminService {
    private final Column[] result = new Column[]{Column.id, Column.username, Column.nickName, Column.mobile, Column.avatar, Column.roleIds, Column.shopId, Column.addTime, Column.location, Column.email, Column.code, Column.socialSecurityNumber, Column.gender, Column.birthday,Column.locationCountry,Column.locationProvince,Column.locationCity,Column.postalCode};
    @Resource
    private LitemallAdminMapper adminMapper;

    public List<LitemallAdmin> selectList(Integer shopId){
        LambdaQueryChainWrapper<LitemallAdmin> queryChainWrapper = new LambdaQueryChainWrapper<>(adminMapper);
        return queryChainWrapper.eq(shopId!=null,LitemallAdmin::getShopId,shopId)
                .list();
    }


    public List<LitemallAdmin> findAdmin(String username) {
        LitemallAdminExample example = new LitemallAdminExample();
        example.or().andUsernameEqualTo(username).andDeletedEqualTo(false);
        return adminMapper.selectByExample(example);
    }

    public LitemallAdmin findAdmin(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }

    public IPage<LitemallAdmin> querySelective(String nickname, Integer shopId, Integer roleId, Integer page, Integer limit, String sort, String order) {
        LambdaQueryWrapper<LitemallAdmin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(nickname),LitemallAdmin::getNickName,nickname);
        lambdaQueryWrapper.like(roleId != null,LitemallAdmin::getRoleIds,new Integer[]{roleId});
        lambdaQueryWrapper.eq(null != shopId,LitemallAdmin::getShopId,shopId);
        lambdaQueryWrapper.eq(LitemallAdmin::getDeleted,0);
        lambdaQueryWrapper.orderByDesc(LitemallAdmin::getAddTime);
        return adminMapper.selectPage(new Page<>(page,limit),lambdaQueryWrapper);
    }

    public int updateById(LitemallAdmin admin) {
        admin.setUpdateTime(LocalDateTime.now());
        return adminMapper.updateByPrimaryKeySelective(admin);
    }

    public void deleteById(Integer id) {
        adminMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallAdmin admin) {
        admin.setAddTime(LocalDateTime.now());
        admin.setUpdateTime(LocalDateTime.now());
        adminMapper.insertSelective(admin);
    }

    public LitemallAdmin findById(Integer id) {
        return adminMapper.selectByPrimaryKeySelective(id, result);
    }

    public List<LitemallAdmin> findByIds(List<Integer> ids) {
        LitemallAdminExample example = new LitemallAdminExample();
        LitemallAdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(ids);
        return adminMapper.selectByExampleSelective(example, result);
    }

    public List<LitemallAdmin> findByShopId(Integer shopId) {
        LitemallAdminExample example = new LitemallAdminExample();
        LitemallAdminExample.Criteria criteria = example.createCriteria();
        criteria.andShopIdEqualTo(shopId).andDeletedEqualTo(false);
        return adminMapper.selectByExampleSelective(example, result);
    }

    public LitemallAdmin findShopMember(Integer shopId, Integer role) {
        List<LitemallAdmin> byShopId = findByShopId(shopId);
        for (LitemallAdmin admin :
                byShopId) {
            for(Integer roleId : admin.getRoleIds()){
                if(role == roleId){
                    return admin;
                }
            }
        }
        return null;
    }

    public List<LitemallAdmin> all() {
        LitemallAdminExample example = new LitemallAdminExample();
        example.or().andDeletedEqualTo(false);
        return adminMapper.selectByExample(example);
    }

    public List<LitemallAdmin> loginHistory() {
        return adminMapper.loginHistory();
    }

    public Map<Integer, LitemallAdmin> getAdminMap(List<Integer> ids) {
        if(CollUtil.isEmpty(ids)) return CollUtil.newHashMap();
        return CollectionUtils.convertMap(adminMapper.selectBatchIds(ids), LitemallAdmin::getId);
    }
}
